Hey there.
Welcome back and thanks for joining me.
We're going to continue where we left off with our wave system by adding sound effects, giving money
to our players when they complete a wave, and highlighting the last alive zombie on the map inside
of the workspace, I have a folder that was originally within our assets pack called sounds.
Inside of here are going to be the different sounds I want to play when a wave is going on.
For example, when we have a wave in progress, we're going to randomly pick one of these sound instances
to play, such as this song or this song.
We'll just randomly pick one of these sounds to play during a wave, and when we're in the intermission,
we're going to be playing this intermission.
Music.
When the players win, a game will play this victory sound victory.
And then when they fail or all the players die and they lose, we'll play this sound instead.
And then at the start of every single round, before we start spawning zombies on the map, I want to
go ahead and play this sound.
So let's go ahead and implement these sounds as well as a couple of other things into our wave system.
So inside of our game handler, what I'm going to do is I'm going to create a new section first to define
the different kind of cash rewards I want to give out to players when they complete a wave.
So I'm going to call this wave Cash Rewards.
And then we can have a key for each wave and what the reward should be.
For example, all the players that survive wave one will give them like 20 bucks for wave two.
We'll also give them 20 bucks for wave three.
We could do 20 bucks, wave four, wheel up it to 30 bucks for wave five.
We'll set that to 30 as well.
Wave six.
We'll keep it at 30 and then wave seven.
We're going to set that to 40.
Wave eight we'll set to 50.
Wave nine we'll set to 60.
And then for wave ten we'll set that to 70.
These are just some random values I'm sitting here.
You can of course change these to whatever you'd like when you want to go and balance out your game.
But I'm just going to leave them at these values for now.
And I'm going to make some variables to refer to the different sounds that are in my workspace.
So we'll get that sounds folder, which is just equal to workspace dot sounds.
And then we're also going to get all of the possible music we can play for a wave.
So we'll call this wave music.
And that's equal to our sounds folder dot wave music.
And we're going to get all the children in there.
And then I want to keep track of what song is currently playing, whether that's the intermission music
or if that's a song playing during a wave.
We'll call it current playing music.
And that's because when I need to fade out the music and switch it to the intermission.
So, for example, let's say we're in a wave and we're killing zombies, and then we kill all the zombies,
and then we switch to the intermission stage.
Well, we want to fade out the music that was playing during the wave and fade it into the intermission
music, so we can go ahead and create a dedicated function for doing that.
I'll go down and create a new function.
And I'm going to call this function fade sound.
And we're just going to pass a sound instance to this function that we should either fade in or fade
out.
And we can check if we need to fade it in or fade it out if the sound is plain.
So if the sound is plain, then that means we should stop playing the sound.
But first we need to fade it out.
So what I'm going to do is I'm going to use the tween service to fade the volume of my sound instance.
So let's go ahead and get the tween service.
And I also want to be able to keep track of the original volume of this sound instance that gets passed
to the function.
So first we're going to create our tween using the tween service create on our sound.
We could do a tween info of like three seconds.
And we want to set the volume equal to zero.
Now before we start playing this tween we're going to go ahead and set an attribute on our sound instance.
And we can call it original volume.
And we're going to set it equal to the sound's original volume.
That way we can always keep track of it.
Then we can go ahead and play this fade tween.
And then I also want this function to yield because let's say in the section where we're starting a
wave, I want to play that howling sound.
And before I start spawning zombies on the map, I want to wait for that howling sound to have fully
faded in.
So we're going to wait for the completed event for our tween to fire.
So we'll call wait on it.
And of course, this is for when we're fading out a sound.
So when we're fading out, we're going to wait.
And then once the event fires, then we can go ahead and stop playing the sound.
Otherwise, if the sound is not playing, we need to fade it in and start playing it.
So what I'm going to do is I'm going to check if the volume of the sound.
Is not equal to zero.
If it's not equal to zero, then we need to go ahead and set an original volume attribute on our sound.
So sound set attribute original volume.
We're going to set that to sound dot volume.
And then we can go ahead and set the sound's volume equal to zero.
And then we'll play the sound.
And here we'll use the tween service once more.
So I'll call this Fade Tween, and we'll create a new tween on the sound instance with a tween info
dot new of three seconds.
And we're going to fade the volume to be equal to the sound's original volume.
So we'll do sound git attribute original volume.
And then we can go ahead and play this tween, and then we'll wait for this tween to finish.
So we'll do completed and then we'll yield right here.
Now, for example, what we can do when we first start our game.
We want to go ahead and start playing the intermission music that is in our workspace.
So after we have told the players that we're waiting for someone to join the live team, after a player
has joined the live team and we start the countdown, then we can go ahead and start playing the intermission
music.
So I'm going to set currently playing music equal to our sounds folder.
Intermission.
And then I don't want to yield here for the tween to finish fading in this intermission music.
I want to go straight away to my countdown, so I'll just spawn my fade sound function in a new thread
and we can just pass our fade sound function.
And then the other argument we need to provide to our fade sound function is that sound instance.
So we'll just pass current playing music and this will fade our intermission music to start playing.
And then what we can go ahead and do is when we want to start a wave, we can play one of the howling
sounds for the horde.
So what I'm going to do is I'm going to use my game event, and I'm first going to fire to all my players
that I want them to update some text on their screen, and I want to tell them that a wave is starting.
So we'll say wave starting, and then we'll play our howling sound.
So we'll do sounds folder dot horde howl will play that.
And then we're going to fade the current playing music.
So if we were in the intermission stage and now we need to go into the zombie fighting stage, then
we're going to fade out the intermission music and we're going to wait for that.
Then what we can go ahead and do is update our currently playing music to a new sound instance.
So inside of our wave music folder, we can go ahead and pick a random one of those sound instances.
So we'll do RNG next integer one to the length of wave music.
And then we're going to fade that music in and we're going to not yield.
So I'll spawn our fade sound function in a new thread and pass our currently playing music.
So this will fade the intermission music out, and then we randomly pick a song to play for this wave,
and then we start playing it.
Then when we get placed inside an intermission, we need to go ahead and fade out the current playing
music.
So if we just came from a wave into the intermission, we need to fade out that wave music and then
fade in the intermission music again.
So what we can go ahead and do here is I'm going to not yield.
So I'll spawn the fade sound function and a new thread.
And we're going to fade out the current playing music.
And then we're going to set current playing music equal to the sounds folder dot intermission.
And then we're going to fade that sound in at the same time.
So we'll do fade sound and then we'll pass current playing music.
So this will fade the intermission music back into the game.
And then what I want to do is when we lose the game, I want to fade out any music that's currently
playing, whether it's the intermission music or it's the wave music, I want to completely fade it
out and then I want to play either that failure sound or the victory sound that I showed you earlier.
So when we lose the game, we want to fade out whatever current music that is playing.
And then inside of our sounds folder, we want to play that failure sound.
And then we can copy this and do the exact same thing in the game over win function.
But this time we want to play our victory sound.
And I believe that's all the sounds we're going to implement for our wave system.
Now, the next thing I said I wanted to do is I wanted to award money to players when they complete
a particular wave.
So we're going to be using our wave cash rewards, and we're going to go through all the players and
award them however much money for a particular wave they completed.
What we're going to go ahead and do is we're going to our function that listens to when a zombie dies.
And before we set the game state to the intermission state to start a new wave, we should go ahead
and award all of our players for completing this particular wave.
So let's go ahead and loop through every single player that's in our game.
So we're going to do players, get players.
And what we're going to do is we're going to refer to this player's leaderstats folder, get their money
and then get the value and increment it by our wave cash rewards for the current wave that we're at.
So we're going to pass wave and then index this with the current wave.
So all of our players are going to get the money for the wave they completed.
Now, the last thing I said I wanted to do is I wanted to create a highlight around the last zombie
that's alive on our map.
So if you know what a highlight instance is, if, for example, I add a highlight instance to this
park bench, it highlights the park bench and you can see the park bench even through other parts or
through other areas.
So that way all the players on the map can see where the last zombie is located.
So what we can go ahead and do is inside of our died event, inside of the function.
Listening to that event, we can check if the number of zombies that are left is equal to one.
Then we can go ahead and create a highlight on that last zombie.
And the way we can do that is by looping through every single zombie that's inside of our zombies workspace
folder.
So we'll get all of the children in there.
And we're going to try to find the zombie in this folder that is alive.
So we can check this zombie that's in the folder.
And if there humanoid's health is greater than zero, then that means yes, they are definitely alive.
So we can go ahead and create a highlight over this zombie.
So let's go ahead and create a function for doing that.
I'll call this function.
Create highlight and we'll get past the parent for this highlight as well as any properties we would
like to manipulate for this highlight.
And we'll create a new highlight instance that's equal to instance dot new highlight.
And then for any of the properties that we get past here, we'll loop through every single property
and value.
In the properties and we'll apply that on our highlight.
And then we can go ahead and set the parent of that highlight equal to that zombie and return highlight
just in case we need it.
Now we can go ahead and go back down.
And when we find our live zombie, we can go ahead and call our create highlight function on this zombie.
So the parent is going to be the zombie.
And then one of the properties I want to manipulate is going to be the fill color property.
I want to go ahead and change this to a different color from the default of red.
And instead I'm going to pick like a yellowish orange type color, something like that.
And then I'm actually going to merge these two into an if or else if statement.
So if the amount of zombies left is equal to one, we'll do this.
Otherwise, if the number of zombies left is equal to zero and we're still in the wave in progress state,
then we'll go ahead and move to the next wave.
So now our wave system should be totally complete.
We should have sound effects playing like music, and we should highlight the last zombie that's alive.
And we should also earn money from whenever we complete a wave.
So to make this easier to test, I'm going to go ahead and decrease these intermission values.
I'm just going to set them to five seconds so I don't have to wait as long.
And then we can go and play test our game.
We'll hit play here.
Okay.
Waves are starting in three, two, one.
Let's go ahead and kill these zombies.
And.
Okay.
Perfect.
We had the horrid sound play.
If you heard earlier, we also had our intermission music.
And now we are having the wave music playing for this first wave.
So let me go ahead and kill all these guys.
Okay, we've got three zombies left.
If I kill these first two, hopefully the last zombie should have a highlight applied around him.
So once I kill this guy, there you go.
As you can see, a highlight has been created around the last zombie in our game.
So I'm still able to see the zombie even through these walls and that notifies all the players.
Okay, the last zombie is here.
Let's go kill him.
And then when I kill this zombie, hopefully I should get awarded some extra money.
There we go.
So, as you can see, I got awarded a bunch of extra money for killing that zombie as well as for for
the wave ending.
And we already went through the intermission.
But you did hear the intermission music and now we are in wave two and now I have 20 zombies to fight.
And now a different wave music is playing.
Now, if all of these zombies kill me, it should play that.
Uh.
We failed.
There we go.
We failed.
Game over.
Zombies win and I can't spawn into the map.
And hopefully all these guys will get cleaned up.
There we go.
They all get deleted.
We're cleaning up the game.
Starting a new game in eight seconds.
So, so far it seems that our wave loop system is working just fine.
When I press this play button, it should start to play the intermission music as you heard.
So let's go hop back in and there's the intermission music playing.
And once this timer is up, it should play the zombie howl.
There we go.
And now we are playing the wave music.
So everything is working well.
All right.
That's all I have for you in this lecture.
And I'll see you in the next one.